package com.juzhi.dvrapp.callback;

import android.app.Activity;
import android.view.View;

import com.juzhi.dvrapp.widget.LoadingActivityDialog;
import com.lzy.okgo.request.base.Request;


public abstract class DialogCallback<T> extends JsonCallback<T> {

    private View view;
    private LoadingActivityDialog dialog;

    private void initDialog(Activity activity) {
        /*dialog = new ProgressDialog(activity);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dialog.setMessage("请求网络中...");*/

        dialog = LoadingActivityDialog.createDialog(activity);
    }
    private void initDialog(Activity activity ,int message) {
        /*dialog = new ProgressDialog(activity);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dialog.setMessage("请求网络中...");*/

        dialog = LoadingActivityDialog.createDialog(activity, message);
    }

    public DialogCallback(Activity activity, View view) {
        super();
        initDialog(activity);
        this.view = view;
    }
    public DialogCallback(Activity activity){
        super();
        initDialog(activity);
    }

    public DialogCallback(Activity activity,int message){
        super();
        initDialog(activity,message);
    }


    @Override
    public void onStart(Request<T, ? extends Request> request) {
        super.onStart(request);
        if (dialog != null && !dialog.isShowing()) {
            dialog.show();
        }
        if (view !=null)
            view.setEnabled(false);
    }

    @Override
    public void onFinish() {
        //网络请求结束后关闭对话框
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
        if (view != null)
            view.setEnabled(true);
    }
}
